package com.apes.hr.entry;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: EntryInformation
 * @Description: 入职资料补充（资料上传，家庭成员）
 * @author: ZhengZeXian
 * @create: 2021/3/12 16:28
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EntryInformation.CONST_TABLE_NAME)
public class EntryInformation extends Model {
    public static final String CONST_TABLE_NAME = "hr_entry_information";

    @Id
    @Column(name = "id", length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "员工资料补充编码")
    @Comment("入职资料补充单号")
    private String id;

    @ManyToOne
    @JoinColumn(name = "employee_id", referencedColumnName="id")
    @Comment("员工")
    private Employee employee;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("员工所属行政部门")
    private Branch branch;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "delete", name = "删除"),
            @State(id = "transApprove", name = "转审核"),
            @State(id = "approve", name = "审核"),
    })
    @Column(name = "state", length = 16, nullable = false)
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveId;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "identity_card_photos", columnDefinition = "CLOB")
    @Comment("身份证正面")
    private String identityCardPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "identity_card_photos_r", columnDefinition = "CLOB")
    @Comment("身份证反面")
    private String identityCardPhotosR;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "one_inch_photos", columnDefinition = "CLOB")
    @Comment("白底一寸照片")
    private String oneInchPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "physical_photos", columnDefinition = "CLOB")
    @Comment("体检报告")
    private String physicalPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "bank_card_photos", columnDefinition = "CLOB")
    @Comment("银行卡正面")
    private String bankCardPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "bank_card_photos_r", columnDefinition = "CLOB")
    @Comment("银行卡反面")
    private String bankCardPhotosR;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "driving_photos", columnDefinition = "CLOB")
    @Comment("驾驶证正面")
    private String drivingPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "driving_photos_r", columnDefinition = "CLOB")
    @Comment("驾驶证反面")
    private String drivingPhotosR;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "job_photos", columnDefinition = "CLOB")
    @Comment("职业资格证")
    private String jobPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "diploma_photos", columnDefinition = "CLOB")
    @Comment("毕业证")
    private String diplomaPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "veteran_photos", columnDefinition = "CLOB")
    @Comment("退伍军人证")
    private String veteranPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "handicapped_photos", columnDefinition = "CLOB")
    @Comment("残疾人证")
    private String handicappedPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "quit_photos", columnDefinition = "CLOB")
    @Comment("离职证明")
    private String quitPhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "disease_photos", columnDefinition = "CLOB")
    @Comment("职业病因素体检报告")
    private String diseasePhotos;

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "other_prove_photos", columnDefinition = "CLOB")
    @Comment("其它证明照片")
    private String otherProvePhotos;

    @OneToMany(mappedBy = "entryInformation", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("家庭成员")
    private List<EntryInformationFamily> entryInformationFamilies = new ArrayList<>();

    public List<EntryInformationFamily> getEntryInformationFamilies() {
        for (EntryInformationFamily entryInformationFamily : entryInformationFamilies) {
            entryInformationFamily.setEntryInformation(this);
        }
        return entryInformationFamilies;
    }

    public void setEntryInformationFamilies(List<EntryInformationFamily> entryInformationFamilies) {
        if (entryInformationFamilies == null) entryInformationFamilies = new ArrayList<>();
        this.entryInformationFamilies.clear();
        this.entryInformationFamilies.addAll(entryInformationFamilies);
    }

    @OneToMany(mappedBy = "entryInformation", cascade=CascadeType.ALL, orphanRemoval = true)
    @Comment("技能证书")
    private List<EntryInformationSkill> entryInformationSkills = new ArrayList<>();

    public List<EntryInformationSkill> getEntryInformationSkills() {
        for (EntryInformationSkill entryInformationSkill : entryInformationSkills) {
            entryInformationSkill.setEntryInformation(this);
        }
        return entryInformationSkills;
    }

    public void setEntryInformationSkills(List<EntryInformationSkill> entryInformationSkills) {
        this.entryInformationSkills.clear();
        if (entryInformationSkills != null) {
            this.entryInformationSkills.addAll(entryInformationSkills);
        }
    }
}
